﻿Imports DevExpress.XtraPrinting

Public Class frmControlCreditosVigentes
    Dim ultExpandido, padreExpandido As Integer
    Private Sub frmControlCreditosVigentes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: esta línea de código carga datos en la tabla 'MEGASTOREDataSet.SP_MonitoreoCreditosDiarioVigentes' Puede moverla o quitarla según sea necesario.
        Try
            Me.SP_MonitoreoCreditosDiarioVigentesTableAdapter.Fill(Me.MEGASTOREDataSet.SP_MonitoreoCreditosDiarioVigentes)

            If frmPrincipal.NivelSeguridad = 1 Then
                NotaDeCréditoToolStripMenuItem.Enabled = True
                NotaDeToolStripMenuItem.Enabled = True
                btnCmsPagado.Enabled = True
                RevertirPagoToolStripMenuItem.Enabled = True
            ElseIf frmPrincipal.NivelSeguridad = 2 Then
                btnCmsPagado.Enabled = True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        

    End Sub

    Public Sub ActualizarGrilla()
        Me.SP_MonitoreoCreditosDiarioVigentesTableAdapter.Fill(Me.MEGASTOREDataSet.SP_MonitoreoCreditosDiarioVigentes)

        Me.gvCreditos.ExpandGroupRow(padreExpandido, True)
        Me.gvCreditos.ExpandGroupRow(ultExpandido, True)
    End Sub

    Private Sub btnCmsPagado_Click(sender As Object, e As EventArgs) Handles btnCmsPagado.Click
        If Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "IdEstado") = 1 Then
            frmPago.IdDetalle = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "IdDetallePlanPago")
            frmPago.Abono = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "Abono")
            frmPago.txtCuotaMensual.EditValue = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "CuotaMensual")
            frmPago.txtInteresMora.EditValue = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "InteresesMora")
            frmPago.txtNotaCredito.EditValue = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "Credito")
            frmPago.txtNotaDebito.EditValue = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "Debito")
            frmPago.txtTotalPagar.EditValue = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "CuotaTotal")
            frmPago.txtAbono.EditValue = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "Abono")
            frmPago.txtPagoRecibido.EditValue = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "CuotaTotal")
            frmPago.ShowDialog()
        Else
            MessageBox.Show("Este pago ya ha sido realizado", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Sub gvCreditos_RowCountChanged(sender As Object, e As EventArgs) Handles gvCreditos.RowCountChanged
        'Aqui vamos a hacer todos los calculos.
        If gvCreditos.RowCount > 0 Then
            CalculoDeInteresesMoratorios()
        End If
    End Sub

    Private Sub CalculoDeInteresesMoratorios()
        Dim InteresCalc, CuotaTotal, Credito, Debito, Abono As Double
        Dim DiasVencidos As Integer
        Dim OtrosDiasAver, FechaPagado1, FechaPago1 As Date
        Dim diasPasadosPagados As Integer

        Dim DiasVencGuardar As Integer

        For i As Integer = 0 To Me.gvCreditos.DataRowCount - 1

            DiasVencidos = Me.gvCreditos.GetRowCellValue(i, "DiasVencidos")
            If DiasVencidos < 0 Then
                DiasVencidos = 0
                Me.gvCreditos.SetRowCellValue(i, colDiasVencidos, Math.Abs(DiasVencidos))
            End If
            If IsDBNull(Me.gvCreditos.GetRowCellValue(i, "Debito")) Then
                Debito = 0
            Else
                Debito = Me.gvCreditos.GetRowCellValue(i, "Debito")
            End If

            If IsDBNull(Me.gvCreditos.GetRowCellValue(i, "Abono")) Then
                Abono = 0
            Else
                Abono = Me.gvCreditos.GetRowCellValue(i, "Abono")
            End If

            If IsDBNull(Me.gvCreditos.GetRowCellValue(i, "Credito")) Then
                Credito = 0
            Else
                Credito = Me.gvCreditos.GetRowCellValue(i, "Credito")
            End If
            If Me.gvCreditos.GetRowCellValue(i, "IdEstado") = 1 Then

                If IsDBNull(Me.gvCreditos.GetRowCellValue(i, "FechaPagado")) Then
                    'Calcula en base los dias vencidos que genera la db
                Else
                    If DiasVencidos > 0 Then
                        OtrosDiasAver = Me.gvCreditos.GetRowCellValue(i, "FechaPagado")
                        DiasVencidos = (Date.Today.Date - OtrosDiasAver).TotalDays
                        'MsgBox(DiasVencidos)
                    End If
                End If
                InteresCalc = (((Me.gvCreditos.GetRowCellValue(i, "CuotaMensual") - Abono) * 0.03) / 30) * DiasVencidos
                'InteresCalc = 0

                If InteresCalc < 0 Then
                    InteresCalc = 0
                End If
                CuotaTotal = (Me.gvCreditos.GetRowCellValue(i, "CuotaMensual") + Debito + InteresCalc) - (Credito + Abono)
                DiasVencGuardar = DiasVencidos
                Me.DetallePlanPagoTableAdapter1.SP_ActualizarInteresCuotaTotalXPlanPago(InteresCalc, CuotaTotal, Me.gvCreditos.GetRowCellValue(i, "IdDetallePlanPago"), DiasVencGuardar)
                Me.gvCreditos.SetRowCellValue(i, "InteresesMora", InteresCalc)
                Me.gvCreditos.SetRowCellValue(i, "CuotaTotal", CuotaTotal)
            Else
                FechaPagado1 = Me.gvCreditos.GetRowCellValue(i, "FechaPagado")
                FechaPago1 = Me.gvCreditos.GetRowCellValue(i, "FechaPago")
                diasPasadosPagados = (FechaPagado1 - FechaPago1).TotalDays
                Me.gvCreditos.SetRowCellValue(i, colDiasVencidos, diasPasadosPagados)
                Me.gvCreditos.SetRowCellValue(i, colDiasVencidos1, diasPasadosPagados)
            End If
            Me.gvCreditos.SelectRow(i)
            Me.gvCreditos.UpdateCurrentRow()
        Next i
    End Sub

    Private Sub NotaDeCréditoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NotaDeCréditoToolStripMenuItem.Click
        If Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "IdEstado") = 1 Then
            Dim Credito2 As String
            Dim Credito As Double
            Credito2 = InputBox("Ingrese el valor de la Nota de Crédito.", varTitulo, "0.00")
            If Credito2 = "" Or Credito2 = "0" Or Credito2 = "0.0" Then
                Credito = 0
            Else
                Credito = CDbl(Credito2)
            End If

            Me.DetallePlanPagoTableAdapter1.SP_AgregarNotaCredito(CDbl(Credito), Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "IdDetallePlanPago"))
            ActualizarGrilla()
        Else
            MessageBox.Show("¡Esta cuota ya ha sido pagada!", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub NotaDeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NotaDeToolStripMenuItem.Click
        If Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "IdEstado") = 1 Then
            Dim Debito As Double
            Dim Debito2 As String
            Debito2 = InputBox("Ingrese el valor de la Nota de Débito", varTitulo, "0.00")

            If Debito2 = "" Or Debito2 = "0" Or Debito2 = "0.0" Then
                Debito = 0
            Else
                Debito = CDbl(Debito2)
            End If

            Me.DetallePlanPagoTableAdapter1.SP_AgregarNotaDebito(CDbl(Debito), Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, "IdDetallePlanPago"))
            ActualizarGrilla()
        Else
            MessageBox.Show("¡Esta cuota ya ha sido pagada!", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnExportarWord_Click(sender As Object, e As EventArgs) Handles btnExportarWord.Click
        If sfdWord.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Dim ps As New PrintingSystem()
            Dim link As New PrintableComponentLink(ps)
            link.Component = gcCreditos
            link.Landscape = False
            link.CreateDocument()
            link.PrintingSystemBase.ExportToRtf(sfdWord.FileName)
        End If
    End Sub

    Private Sub bnExportaXLS_Click(sender As Object, e As EventArgs) Handles bnExportaXLS.Click
        If sfdExcel.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Dim ps As New PrintingSystem()
            Dim link As New PrintableComponentLink(ps)
            link.Component = gcCreditos
            link.Landscape = False
            link.CreateDocument()
            link.PrintingSystemBase.ExportToXls(sfdExcel.FileName)
        End If
    End Sub

    Private Sub btnExportaPDF_Click(sender As Object, e As EventArgs) Handles btnExportaPDF.Click
        If sfdPDF.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Dim ps As New PrintingSystem()
            Dim link As New PrintableComponentLink(ps)
            link.Component = gcCreditos
            link.Landscape = False
            link.CreateDocument()
            link.PrintingSystemBase.ExportToPdf(sfdPDF.FileName)
        End If
    End Sub

    Private Sub RevertirPagoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RevertirPagoToolStripMenuItem.Click
        RevertirPago()
    End Sub

    Private Sub RevertirPago()
        Dim resp As String = MessageBox.Show("¿Está seguro de revertir el pago seleccionado?", varTitulo, MessageBoxButtons.YesNo, MessageBoxIcon.Warning)

        If resp = vbYes Then
            frmRevertirPago.Usuario = frmPrincipal.UsuarioLogin
            frmRevertirPago.CodCliente = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colCodCliente)
            frmRevertirPago.Cliente = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colNombreCliente)
            frmRevertirPago.Monto = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colCuotaTotal)
            frmRevertirPago.NumFactura = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colNumFactura)
            frmRevertirPago.NumCuota = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colNumCuota)
            frmRevertirPago.IdDetallePago = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colIdDetallePlanPago)
            frmRevertirPago.ShowDialog()
        End If
    End Sub

    Private Sub gvCreditos_GroupRowExpanded(sender As Object, e As DevExpress.XtraGrid.Views.Base.RowEventArgs) Handles gvCreditos.GroupRowExpanded
        ultExpandido = e.RowHandle
        padreExpandido = gvCreditos.GetParentRowHandle(ultExpandido)
    End Sub

End Class